package svgedit.gui.command;

import svgedit.gui.Frame;
import svgedit.svg.SVGDocument;

/**
 *  Define change document size execution as a command object which has 
 *      execute (redo) and undo methods
 * */
public class DocumentSizeCommand implements Command{

	private SVGDocument document;
	private float width = 0;
	private float height = 0;
	private String changeWidth;
	private String changeHeight;
	private Frame frame;
	
	/** Constructor for document size change command
	 * 
	 * @param document get the document that is to be changed 
	 * @param frame get the singleton frame object
	 * @param changeWidth to get the change width in textfield
	 * @param changeHeight to get the change height in textfield
	 * */
	public DocumentSizeCommand(Frame frame,SVGDocument document , String changeWidth, String changeHeight){
		this.frame = frame;
		this.document = document;
		this.changeHeight = changeHeight;
		this.changeWidth = changeWidth;
	}
	
	/** 
	 * To execute change document size function or when user undo back and redo it
	 * */
	public void execute(){
		if (width == 0 && height == 0){
			width = document.getWidth().getValueInSpecifiedUnits();
			height = document.getHeight().getValueInSpecifiedUnits();
			document.getWidth().setValueFromString(changeWidth);
	        document.getHeight().setValueFromString(changeHeight);
		}else {
			document.getWidth().setValueFromString(changeWidth);
	        document.getHeight().setValueFromString(changeHeight);
	        frame.getView().repaint();
		}
	}
	
	/**
	 *  Undo the execution , when change document size, undo this execution to change back the size
	 * */
	public void undo(){
		document.getWidth().setValue(width);
        document.getHeight().setValue(height);
        frame.getView().repaint();
	}
}
